package dcc.component;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

import com.l2fprod.common.swing.JLinkButton;
import com.l2fprod.common.swing.JTaskPane;

import dcc.mbg.model.BaseInfo;
import dcc.tools.Tools;

public class JDataExportPanel extends JPanel {

	public JDataExportPanel() {
		taskPaneGroupList = new ArrayList<DataExportTaskPaneGroup>();
		setLayout(new BorderLayout(0, 0));
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane, BorderLayout.CENTER);

		JPanel panel = new JPanel();
		scrollPane.setViewportView(panel);
		panel.setLayout(new GridLayout(0, 1, 0, 0));

		JTaskPane taskPane = new JTaskPane();
		taskPane.setBorder(new EmptyBorder(2, 2, 2, 2));
		panel.add(taskPane);

		JPanel topPanel = new JPanel();
		topPanel.setPreferredSize(new Dimension(0, 48));
		add(topPanel, BorderLayout.NORTH);
		GridBagLayout gbl_topPanel = new GridBagLayout();
		gbl_topPanel.columnWidths = new int[] { 32, 0, 0, 0 };
		gbl_topPanel.rowHeights = new int[] { 0, 0 };
		gbl_topPanel.columnWeights = new double[] { 0.0, 0.0, 0.0,
				Double.MIN_VALUE };
		gbl_topPanel.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
		topPanel.setLayout(gbl_topPanel);

		JLinkButton btnSelectAll = new JLinkButton("\u9009\u4E2D\u5168\u90E8");
		btnSelectAll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// TODO select all records
				selectAll();
			}
		});
		btnSelectAll.setFont(new Font("Microsoft YaHei", Font.PLAIN, 18));
		btnSelectAll.setIcon(new ImageIcon(JDataExportPanel.class
				.getResource("/dcc/resources/images/select_all.png")));
		GridBagConstraints gbc_btnSelectAll = new GridBagConstraints();
		gbc_btnSelectAll.insets = new Insets(0, 0, 0, 5);
		gbc_btnSelectAll.anchor = GridBagConstraints.WEST;
		gbc_btnSelectAll.fill = GridBagConstraints.VERTICAL;
		gbc_btnSelectAll.gridx = 0;
		gbc_btnSelectAll.gridy = 0;
		topPanel.add(btnSelectAll, gbc_btnSelectAll);

		JLinkButton btnCancel = new JLinkButton("\u53D6\u6D88\u9009\u4E2D");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// TODO cancel selects
				unSelectAll();
			}
		});
		btnCancel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 18));
		btnCancel.setIcon(new ImageIcon(JDataExportPanel.class
				.getResource("/dcc/resources/images/un_select.png")));
		GridBagConstraints gbc_btnCancel = new GridBagConstraints();
		gbc_btnCancel.insets = new Insets(0, 0, 0, 5);
		gbc_btnCancel.fill = GridBagConstraints.BOTH;
		gbc_btnCancel.gridx = 1;
		gbc_btnCancel.gridy = 0;
		topPanel.add(btnCancel, gbc_btnCancel);

		JLinkButton btnExport = new JLinkButton("\u5BFC\u51FA");
		btnExport.setFont(new Font("Microsoft YaHei", Font.PLAIN, 18));
		btnExport.setIcon(new ImageIcon(JDataExportPanel.class
				.getResource("/dcc/resources/images/export.png")));
		btnExport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				exportData();
			}
		});
		GridBagConstraints gbc_btnExport = new GridBagConstraints();
		gbc_btnExport.fill = GridBagConstraints.BOTH;
		gbc_btnExport.gridx = 2;
		gbc_btnExport.gridy = 0;
		topPanel.add(btnExport, gbc_btnExport);
		// TODO Create 9 DataExportTaskPaneGroup and add to taskPane
		Iterator<String> iterator = Tools.get().getTalentTypeMapToLabel()
				.keySet().iterator();
		while (iterator.hasNext()) {
			taskPaneGroupList.add(new DataExportTaskPaneGroup(iterator.next()
					.toString(), taskPane));
		}
	}

	private List<DataExportTaskPaneGroup> taskPaneGroupList;

	public void update() {
		for (DataExportTaskPaneGroup taskPaneGroup : taskPaneGroupList) {
			taskPaneGroup.update();
		}
	}
	
	public void selectAll(){
		for (DataExportTaskPaneGroup taskPaneGroup : taskPaneGroupList) {
			taskPaneGroup.selectAll();
		}
	}
	
	public void unSelectAll(){
		for (DataExportTaskPaneGroup taskPaneGroup : taskPaneGroupList) {
			taskPaneGroup.unSelectAll();
		}
	}

	private void exportData() {
		List<DataExportTable> tables = Tools.get().getDataExportTables();
		List<BaseInfo> talents = new ArrayList<BaseInfo>();
		for (DataExportTable dataExportTable : tables) {
			List<BaseInfo> tmpTalents = dataExportTable.getSelectedTalents();
			for (BaseInfo baseInfo : tmpTalents) {
				talents.add(baseInfo);
				System.out.println(baseInfo.getShiBieHao());
			}
		}
		// TODO Call DataExport Model
		try {
			String zipFileName = Tools.get().getDataExportService().exportData(talents);
			if (zipFileName != null){
				JOptionPane.showMessageDialog(null, Tools.get()
						.getValue("LABEL_EXPORT_OK_PROMPT") + zipFileName, Tools.get()
						.getValue("LABEL_PROMPT_TITLE"),
						JOptionPane.INFORMATION_MESSAGE);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
